<?php
class My_API_ss28_Users
{
    private $_config;

    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
    
    public function getUsersByServerId($listUser, $serverId, $channel = ''){
        $i = 0;
		$infoUsers = array();
        $serverIdPartner = str_replace('S','',$serverId);
        
		$userCheck = array_chunk($listUser, 50);
		foreach ($userCheck as $key => $listCheck){				
			//get qid of listuser		
            $postData = array(
                'user' => str_replace("'", "", implode(",", $listCheck)),
                'serverId' => $serverIdPartner
                );
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $this->_config->getUserInfo->url);
            #curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
            $result = curl_exec($curl);
            curl_close($curl);
            $result = json_decode($result);
            
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $key=>$row){
                        
					$infoUsers[$i]['serverId'] = $serverId;
					$infoUsers[$i]['roleName'] = $row->roleName;
					$infoUsers[$i]['roleLevel'] = $row->level;
					$i++;
                }
			}else{
				continue;
			}
		}
        
		return $infoUsers;
    }
    
    public function getUsernameByRolename($listRoleName, $serverId){
        $i = 0;
		$infoUsername = array();
        $serverIdPartner = str_replace("S",'',$serverId);
        
		$userCheck = array_chunk($listRoleName, 50);
		foreach ($userCheck as $listCheck){
            $accounts = implode(',',$listCheck);
            $urlAPI = $this->_config->getUsernameInfo->url . "?rolename=$accounts&server_id={$serverIdPartner}";
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $row){
					$infoUsername[$i]['serverId'] = $serverId;
					$infoUsername[$i]['username'] = $row->username;
					$infoUsername[$i]['roleLevel'] = $row->level;
					$i++;
                }
			}else{
				continue;
			}
		}
		return $infoUsername;
    }
}